package com.tea.room.service.tea.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tea.room.dao.tea.SelectionDao;
import com.tea.room.model.tea.Selection;
import com.tea.room.service.tea.SelectionService;
import com.tea.room.util.DateUtils;

@Service
@Transactional
public class SelectionServiceImpl implements SelectionService {

    @Autowired
    private SelectionDao selectionDao;

    @Override
    public List<Map<String, Object>> findSelection(String tea_code) throws Exception {
        List<Map<String, Object>> newlist = new ArrayList<Map<String, Object>>();
        // 初始化时间段
        List<Map<String, Object>> listMap = list2ListMap();

        //查询当天以预定的时间段
        Selection s1 = new Selection();
        s1.setSame_day(DateUtils.getToDay(0));
        s1.setTeaCode(tea_code);
        Selection today = selectionDao.query(s1);
        //查询第二天以预定的时间段
        Selection s2 = new Selection();
        s2.setSame_day(DateUtils.getToDay(1));
        s2.setTeaCode(tea_code);
        Selection selection = selectionDao.query(s2);
        if(today == null && selection == null){
            return listMap;
        }
        newlist.addAll(listMap);
        if(today != null){
        	String[] sub = today.getSelection().split(",");
        	tag:for (String se : sub) {
        		for (int i = 0; i < listMap.size(); i++) {
        			if(listMap.get(i).get("time").equals("00:00")){
        				continue tag;
        			}
        			if (listMap.get(i).get("time").equals(se)) {
        				Map<String, Object> map = listMap.get(i);
        				map.put("reserve", "1");// 已预订
        				newlist.set(i, map);
        			}
        		}
        	}
        }
        //判断时间有没有跨两天
        int temp = -1;
        for (int i = 0; i < listMap.size(); i++) {
            if (listMap.get(i).get("time").equals("00:00")) {
                temp = i;
            }
        }
        if (temp != -1 && selection != null ) {//查询第二天以预定的时间段
            String[] sub1 = selection.getSelection().split(",");
            for (int i = 0; i < sub1.length; i++) {
                for (int a = temp; a < listMap.size(); a++) {
                    if (listMap.get(a).get("time").equals(sub1[i])) {
                        Map<String, Object> map = listMap.get(a);
                        map.put("reserve", "1");// 已预订
                        newlist.set(a, map);
                    }
                }
            }
        }
        return newlist;
    }

    /**
     * 初始化返回集合
     * 
     * @return
     */
    public List<Map<String, Object>> list2ListMap() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> time = DateUtils.sortTime(DateUtils.getDateTime(11), DateUtils.getDateTime(12));
        for (String str : time) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("time", str);
            map.put("reserve", "0");// 未预定
            list.add(map);
        }
        // 已过期，每个数组第一个都展示当前时间前一个时间段 当前时间12:30 展示12:00
        list.get(0).put("reserve", "1");
        return list;
    }

}
